#include "OgreSystem.h"
#include "GameSystemManager.h"
#include "XMLHandler.h"

Root* OgreSystem::mRoot = NULL;
RenderWindow* OgreSystem::mWindow = NULL;

OgreSystem::OgreSystem() //const
{
	
}
OgreSystem::~OgreSystem()
{


}
void OgreSystem::saveConfigToXML(TiXmlElement* mElem) {


}
void OgreSystem::loadConfigToXML(TiXmlElement* mElem) {


}

//Initialize Ogre API
bool OgreSystem::initialize() {
	
	String pluginsPath;
	// only use plugins.cfg if not static
	#ifndef OGRE_STATIC_LIB
		pluginsPath = "plugins.cfg";
	#endif

	mRoot = new Root(pluginsPath,"ogre.cfg", "Ogre.log");
	
	if(!mRoot)
		return false;
	
	return true;
	//TextureManager::getSingleton().setDefaultNumMipmaps(5);
}

//Load Application resources
bool OgreSystem::loadResources() {
    // Load resource paths from config file
    ConfigFile cf;
    cf.load("resources.cfg");

    // Go through all sections & settings in the file
    ConfigFile::SectionIterator seci = cf.getSectionIterator();

    String secName, typeName, archName;
    while (seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap *settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
			for (i = settings->begin(); i != settings->end(); ++i)
			{

				//if(secName == "Bootstrap" || secName == "General" || secName == "ET") {
				if(secName != "Client") {
					typeName = i->first;
					archName = i->second;
					ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
				}
			}
    }
	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
	TextureManager::getSingleton().setDefaultNumMipmaps(5);
	return true;
}

//Display Ogre condig dialog
bool OgreSystem::configure() {
    if(mRoot->showConfigDialog())
    {
        mWindow = mRoot->initialise(true,"Ape Client");

		if(mWindow)
			mWindow->getCustomAttribute("WINDOW", &hWnd);

		//mRoot->initialise(true,"Ape Client");
        //return true;
    }
	else {
		//return false;
	}
	return true;
}

void OgreSystem::destroy(){
	if (mRoot) {
		delete mRoot;
	}
}

Root* OgreSystem::getRoot(){
	return mRoot;
}

RenderWindow* OgreSystem::getWindow() {
	return mWindow;
}

unsigned long OgreSystem::getWindowHandler() {
	return hWnd;
}

bool OgreSystem::saveToXML() {
	
	XMLHandler *xml = GameSystemManager::getSingleton()->getXMLHandler();

	TiXmlElement* parent = xml->createParentElement("OgreConfig");
	
	TiXmlElement* wnd = xml->createChildElement(parent,"Window");
	wnd->SetAttribute("Name", "Ape Engine Editor");

	TiXmlElement* dialog = xml->createChildElement(parent,"ConfigDialog");
	dialog->SetAttribute("Show", "true");

	TiXmlElement* plugincfg = xml->createChildElement(parent,"PluginCfg");
	plugincfg->SetAttribute("Name", "plugins.cfg");

	TiXmlElement* ogrecfg = xml->createChildElement(parent,"OgreCfg");
	ogrecfg->SetAttribute("Name", "ogre.cfg");

	return true;
}